package com.xpec.c4.service.module.datacenter.player;

import java.util.ArrayList;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.player.Player;
import com.ms.player.PlayerNickNameParser;
import com.ms.service.module.account.Account;
import com.xpec.c4.db.dao.PlayerDAO;
import com.xpec.c4.game.datamanager.ResultNo;
import com.xpec.c4.game.login.async.AccountLoginRequestGetAccountData;
import com.xpec.c4.game.time.TimeSys;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.character.CharacterSampleInfo;
import com.xpec.c4.message.login.SAccountCharacterListResponse;
import com.xpec.c4.service.module.common.PlayerCommonInfoManager;
import com.xpec.c4.service.module.datacenter.DCAsyncCommand;
import com.xpec.c4.service.module.datacenter.DataElement;

/**
 * 
 * @author brian_yang
 *
 */
public class GetPlayerListAsync extends DCAsyncCommand{

	PlayerDataManager dataManager;
	
	/**
	 * 
	 */
	private static final Log log = LogFactory.getLog(GetPlayerListAsync.class);
	
	
	@Override
	public void asyncExecute() {

		AccountLoginRequestGetAccountData tmpData = (AccountLoginRequestGetAccountData) dataInfo
				.getData();

		tmpData.msg = getCharacterListResponse(tmpData.account);

		if (dataInfo.getData() != null) {
			resultCode = ResultNo.OK;
		} else {
			resultCode = ResultNo.DATA_NOT_FOUND;
		}

		return;
	}

	@Override
	public void execute() {
		// callback
		if (dataManager != null) {
			((PlayerDataManager) dataManager).getPlayerListCallback(resultCode,
					dataInfo, srcModuleID, dataInfo.getData());
		}
	}

	/**
	 * 登入時login module、刪除角色後都會call這個
	 * @param account
	 * @return
	 */
	private SAccountCharacterListResponse getCharacterListResponse(Account account){
		
		// PlayerDataManager
		
		SAccountCharacterListResponse msg = new SAccountCharacterListResponse();
		
		msg.characters = new ArrayList<CharacterSampleInfo>();
		msg.systemTime = TimeSys.currentTimeMills();
		if(account.getBankPassword() == null || account.getBankPassword().equals("")){
			msg.bankPassword = false;
		}else{
			msg.bankPassword = true;
		}
		
		Map<GUID, Integer> serialNumMap = account.getPlayerSerialNumMap();
		if (serialNumMap.size() > 0) {
			ArrayList<String> offlineSpinList = PlayerDAO.checkAccountPlayerOffineSpin(serialNumMap.keySet());
			
			boolean hasFirstRewardPlayer = false;
			for (GUID playerid : account.getPlayers()) {
				if (playerid == null) {
					msg.characters.add(null);
				} else {
					// FIXME 這邊應該只是要load player基本資料供顯示使用

					Player player = null;

					DataElement elem = ((PlayerDataManager) this.getCallback()).getData(playerid);

					if (elem != null) {
						player = (Player) elem.getData();
					} else {

						try {
							// player =
							// PlayerManager.getInstance().getPlayerInDBCache(playerid);
							player = PlayerDAO.loadPlayerByGuid(playerid);
						} catch (Exception e) {
							log.error(e + " Load Player failed. " + account.getAccountId() + ":" + playerid);
						}
					}

					if (player != null) {

						CharacterSampleInfo csi = new CharacterSampleInfo();
						csi.guid = player.getGuid();
						csi.guidString = player.getGuid().toString();
						// csi.name = player.getName();
						csi.name = PlayerNickNameParser.mergeNickName(player.getName(), player.getNameNumber());
						csi.level = player.getLevel();
						csi.gold = player.getGoldCoin().toString();
						csi.vip = player.getVip();
						csi.facephoto = player.getFacephoto();

						int serialNum = 0;
						if (serialNumMap != null && serialNumMap.get(player.getGuid()) != null) {
							// 防呆 以免有人忘記轉換
							serialNum = account.getPlayerSerialNumMap()
									.get(player.getGuid())
									.intValue();
						}
						csi.serialNum = serialNum;

						GUID session = PlayerCommonInfoManager.getInstance()
								.getSessionByPlayerGuid(player.getGuid());
						if (session != null) {
							csi.isOnline = true;
						} else {
							csi.isOnline = false;
						}

						if (offlineSpinList.contains(playerid.toString())) {
							csi.isOfflineSpin = true;
						} else {
							csi.isOfflineSpin = false;
						}
						
						if(player.getStoreRewardContinuousLoginCount() > 0){
							hasFirstRewardPlayer = true;
						}

						msg.characters.add(csi);
					}
				}
			}
			msg.hasFirstRewardPlayer = hasFirstRewardPlayer;
		}	
		return msg;
	}

	public void setDataManager(PlayerDataManager dataManager) {
		this.dataManager = dataManager;
	}
	
}
